DataViz avec R
Aline Deschamps

DataViz avec R

Aline Deschamps

12 décembre 2017

Présentation de R

Présentation de R


Présentation de R



Graphiques de base

Fonctions “clé en main”

boxplot(Age ~ Gender, data = donnees,
        main = "Boxplot de l'âge en fonction du Genre",
        col = c("pink", "lightblue"))

Fonctions “clé en main”

tab <- table(donnees$Gender)

barplot(tab, main = "Effectifs par Genre",
        col = c("pink", "lightblue"),
        ylim = c(0, max(tab)+10))

Fonctions “clé en main”

tab <- table(donnees$Gender)

pie(tab, main = "Effectifs par Genre",
        col = c("pink", "lightblue"))

Fonctions “mains dans le cambouis”

Poids <- donnees$Weight
Taille <- donnees$Height

plot(Poids ~ Taille, 
     main = "Poids selon la Taille")

Fonctions “mains dans le cambouis”

plot(Poids ~ Taille, 
     main = "Poids selon la Taille",
     xlab = "Taille (en cm)",
     ylab = "Poids (en kg)",
     cex = 0.8,
     pch = 4, col = "blue",
     col.main = "purple")

Fonctions “mains dans le cambouis”

plot(Poids ~ Taille, 
     main = "Poids selon la Taille",
     xlab = "Taille (en cm)",
     ylab = "Poids (en kg)",
     cex = 0.8,
     pch = 4, col = "blue",
     col.main = "purple")
lines(Taille, 1.06*Taille-117.62, lty = 1, col = "red")
abline(h = mean(Poids, na.rm = TRUE), 
       lty = 3, lwd = 2, col = "darkgreen")

legend(x = 147, y = 164, 
       title = "Légende",
       legend = c("Répondants", "Droite de régression", 
                  "Valeur du Poids moyen"),
       cex = 0.8,
       pch = c(4, NA, NA),
       lty = c(NA, 1, 3),
       lwd = c(1, 1, 2),
       col = c("blue", "red", "darkgreen"))

Les formats d’exports

Il existe différents formats d’exports possibles pour les graphiques en R (qui peuvent dépendre du package utilisé), sont notamment disponibles les formats suivants :

Graphiques un peu plus spécifiques

Graphiques un peu plus spécifiques

name=c(3,10,10,3,6,7,8,3,6,1,2,
       2,6,10,2,3,3,10,4,5,9,10)
feature=paste("feature ", c(1,1,2,2,2,2,2,3,3,3,
              3,3,3,3,4,4,4,4,5,5,5,5) , sep="")
dat <- data.frame(name, feature)
dat <- with(dat, table(name, feature))
 
library(circlize)

chordDiagram(as.data.frame(dat), transparency = 0.5)

Graphiques un peu plus spécifiques

library("igraph")

vertices <- data.frame("name" = unique(unlist(relations))) 
g = graph.data.frame(relations, directed=F, vertices=vertices) 
vertices$group = edge.betweenness.community(g)$membership

set.seed(125)
plot(g,
     layout = layout.auto, 
     vertex.color = vertices$group,
     vertex.label = NA, 
     vertex.size = 5,
     edge.arrow.size = 0.8, main = "Network graph")

Graphiques un peu plus spécifiques

library(fmsb)
 
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , 
                 rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , 
             rgb(0.7,0.5,0.1,0.4) )
radarchart( data  , axistype = 1 , 
    pcol = colors_border , pfcol = colors_in , 
    plwd = 4 , plty = 1,
    cglcol ="grey", cglty = 1, axislabcol = "grey", 
    caxislabels = seq(0,20,5), cglwd = 0.8,
    vlcex = 0.8 , title = "Radar Chart"
    )
legend(x = 0.7, y = 1, legend = rownames(data[-c(1,2),]), 
       bty = "n", pch = 20 , col = colors_in , 
       text.col = "grey", cex = 0.8, pt.cex = 3)

Le package “ggplot2”

Exemple 1 “ggplot2”

library(ggplot2)

ggplot(na.omit(donnees), 
       aes(x = Height, y = Weight, 
           color = Gender, shape = Gender)) + 
  geom_point(size = 2.5, alpha = 0.5) +
  labs(x = "Taille", y = "Poids", 
       title = "Mon Graphique", 
       subtitle = "Taille et Poids par Genre", 
       color = "Genre", shape = "Genre")

Exemple 2 “ggplot2”

library(ggplot2)

ggplot(data, aes(fill = condition, y = value, x = specie)) + 
    geom_bar( stat = "identity", position = "fill")

Exemples avancés

Exemples avancés

Exemples avancés

Exemples avancés

Les couleurs

Les couleurs

Dans R, on peut sélectionner des couleurs par : numéro (1, 2, 3, …), nom (“lightblue”, …), code hexadécimal (#E6E6FA), code RGB (rgb(230, 230, 250, max = 255)), …

Cartographie

Cartographie statique : exemple 1

library(cartography)

data(nuts2006)
nuts2.df$cagr <- 100 * 
                  (((nuts2.df$pop2008/nuts2.df$pop1999)^(1/9)) - 1)

cols <- carto.pal(pal1 = "green.pal", n1 = 2, 
                  pal2 = "red.pal", n2 = 4)

plot(nuts0.spdf, border = NA, col = NA, bg = "#A6CAE0")
plot(world.spdf, col = "#E3DEBF", border = NA, add = TRUE)


choroLayer(spdf = nuts2.spdf, df = nuts2.df, var = "cagr", 
           breaks = c(-2.43, -1, 0, 0.5, 1, 2, 3.1), 
           col = cols, border = "grey40", lwd = 0.5, 
           legend.pos = "right", 
           legend.title.txt = "taux de croissance\n
                                          annuel moyen", 
           legend.values.rnd = 2, add = TRUE)

plot(nuts0.spdf, border = "grey20", lwd = 0.75, add = TRUE)

layoutLayer(title = "Growth rate in Europe", 
            author = "cartography", 
            sources = "Eurostat, 2008", 
            frame = TRUE, col = NA, scale = NULL, 
            coltitle = "black", south = TRUE)

Cartographie statique : exemple 2

library(rgdal)
library(rgeos)
library(ggplot2)
 
us <- readOGR("us_states_hexgrid.geojson", "OGRGeoJSON")
 
centers <- cbind.data.frame(
             data.frame(gCentroid(us, byid=TRUE), 
                        id=us@data$iso3166_2))
 
us_map <- fortify(us, region="iso3166_2")
 
ggplot(data=us_map, aes(map_id=id, x=long, y=lat)) + 
    geom_map(map=us_map, color="black", fill="white")
 
gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
                    aes(x=long, y=lat, map_id=id),
                    color="white", size=0.5)
gg <- gg + geom_map(data=us@data, map=us_map,
                    aes(fill=bees, map_id=iso3166_2))
gg <- gg + geom_map(data=us@data, map=us_map,
                    aes(map_id=iso3166_2),
                    fill="#ffffff", alpha=0, color="white",
                    show_guide=FALSE)
gg <- gg + geom_text(data=centers, aes(label=id, x=x, y=y), 
                     color="white", size=4)
gg <- gg + scale_fill_distiller(palette="RdPu", 
                                na.value="#7f7f7f")
gg <- gg + coord_map()
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme_bw()
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_blank())
gg



Cartographie dynamique avec “leaflet”

library(leaflet)

villes <- data.frame(
  Ville = c("Paris", "Lille", "Nantes", "Marseille"),
  Latitude = c(48.85661400000001, 50.62924999999999, 47.218371, 43.296482),
  Longitude = c(2.3522219000000177, 3.057256000000052, 
                -1.553621000000021, 5.369779999999992),
  Population = c(2249975, 227560, 284970, 850726))

couleurs <- colorNumeric("YlOrRd", villes$Population, n = 5)

m <- leaflet(villes) %>% 
      addTiles() %>%
      addCircles(lng = ~Longitude, lat = ~Latitude, weight = 1,
                 radius = ~sqrt(Population) * 50, 
                 popup = ~paste(Ville, ":", Population),
                 color = ~couleurs(Population), fillOpacity = 0.9) %>%
      addLegend(pal = couleurs, values = ~Population, opacity = 0.9)

m

Graphiques dynamiques (pour le web)

Packages

Il existe de nombreux packages R pour réaliser des graphiques dynamiques à destination du web (reposant sur des librairies JS connues), tels que par exemple :

Exemple 1

library(rAmCharts)

data("data_bar")

amBarplot(x = "country", y = "visits", data = data_bar, 
          show_values = TRUE, labelRotation = -90)

Exemple 2

library(rAmCharts)

data('data_stock_2')

amTimeSeries(data_stock_2, 'date', c('ts1', 'ts2'))

Tableaux de bord dynamiques sur le web (le package “shiny”)

Principe

Shiny : package R permettant de générer une application web dynamique, présentant des résultats statistiques et graphiques réalisés avec R, le tout sans avoir besoin d’aucune connaissance en HTML, CSS ou JavaScript


Principe

Deux fichiers de scripts :

Exemple 1

# ui.R
library(shiny)

shinyUI(
  pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris),
                selected=names(iris)[[2]]),
    numericInput('clusters', 'Cluster count', 3,
                 min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1')
  )
 )
)
# server.R
library(shiny)

function(input, output, session) {

  selectedData <- reactive({
    iris[, c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })

  output$plot1 <- renderPlot({
    palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
      "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })

}

Exemple 1


Lien : kmeans-example.html

Exemple 2

Lien : Demo DACTA

Autres exemples



Ressources

Ressources

Installations :

Packages :

Exemples :